﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

using ComLib;
using ComLib.Data;
using ComLib.Feeds;
using ComLib.Entities;
using ComLib.Web.Lib.Attributes;
using ComLib.Web.Modules.Widgets;


namespace ComLib.CMS.Models.Widgets
{

    /// <summary>
    /// BlogRoll Widget , Path = "Widgets/Maps"
    /// </summary>
    [Widget(Name = "GeoMap", IsCachable = true, IsEditable = true, SortIndex = 5)]
    public class GeoMap : WidgetInstance
    {
        /// <summary>
        /// Initialize.
        /// </summary>
        public GeoMap()
        {
            Name = "map1";
            Provider = "bing";
            Height = 120;
            Width = 120;
        }


        /// <summary>
        /// Name for map instance.
        /// </summary>
        [PropertyDisplay(Order = 1, DefaultValue = "", Description = "The name of the map, used when having multiple maps.")]
        public string Name { get; set; }


        /// <summary>
        /// "bing", "google"
        /// </summary>
        [Required]
        [PropertyDisplay(Order = 2, DefaultValue = "", Description = "Either 'bing' or 'google'")]
        public string Provider { get; set; }


        /// <summary>
        /// The full address in one line separated by comma to represent the address the show.
        /// </summary>
        [Required]
        [PropertyDisplay(Label = "Full Address", Order = 3, DefaultValue = "", Description = "The address to show ( separate parts of address by comma )")]
        public string FullAddress { get; set; }


        /// <summary>
        /// Height of the map
        /// </summary>
        [Range(30, 800)]
        [PropertyDisplay(Order = 4, DefaultValue = "120", Description = "The height of the map")]
        public int Height { get; set; }


        /// <summary>
        /// Width of the map
        /// </summary>
        [Range(30, 800)]
        [PropertyDisplay(Order = 5, DefaultValue = "120", Description = "The width of the map")]
        public int Width { get; set; }


        /// <summary>
        /// Some textual content for the map supplied by user.
        /// </summary>
        [Range(0, 150)]
        [PropertyDisplay(Order = 6, DefaultValue = "", Description = "Optional text to display for map")]
        public string Content { get; set; }


        /// <summary>
        /// Whether or not this widget can render itself.
        /// </summary>
        public override bool IsSelfRenderable
        {
            get { return true; }
        }


        /// <summary>
        /// Renders the html for the widget.
        /// </summary>
        /// <returns></returns>
        public override string DoRender()
        {
            bool isBing = string.IsNullOrEmpty(Provider) || string.Compare(Provider, "bing", true) == 0;
            var providerName = isBing ? "Bing" : "Google";
            var jsFileName = "Maps." + providerName + ".js";
            var jsFullPath = "/scripts/app/" + jsFileName;
            var buffer = new StringBuilder();
            var instanceName = string.IsNullOrEmpty(Name) ? "map1" : Name;
            var mapJsUrl = Configuration.Config.Get<string>("Maps", providerName.ToLower() + ".Url");

            // 1. Register the script into the footer of the cms.
            CMS.Scripts.AddJavascript("provider_" + jsFullPath, mapJsUrl);
            CMS.Scripts.AddJavascript(jsFileName, jsFullPath);
            
            //  <script type="text/javascript">
            //  $(document).ready(function() 
            //  {
            //      var map1 = new <%=javascriptMapName%>Map("map1");
            //      map1.Init();
            //      map1.Locate(mapLocation);
            //  });
            //  </script>
            //  <div id="map1" style="position:relative; width:<%= Model.Width %>px; height:<%= Model.Height %>px;"></div>

            buffer.Append("<script type=\"text/javascript\">");
            buffer.Append("$(document).ready(function() { ");
            buffer.Append("var " + instanceName + " = new " + providerName + "Map('" + instanceName + "');");
            buffer.Append(instanceName + ".Init();");
            buffer.Append(instanceName + ".Locate(\"" + FullAddress + "\");");
            buffer.Append("});");
            buffer.Append("</script>");
            buffer.Append(@"<div id=""" + instanceName + @""" style=""position:relative; width:" + Width + "px; height:" + Height + @"px;""></div>");
            
            var finalHtml = buffer.ToString();
            return finalHtml;
        }
    }
}
